Type
Type-Id Pair
Language Transition
State Transition
Keyword Set
Type Cache
Helper Classes
Line
Code Parser
Visual C++ command-line tool
Overview
The Code Formatter project is a port of the Code Viewer page to C++. The port has two goals. The first is to provide an easier way to test the Code Viewer code. It’s a lot easier to debug a C++ console app than a PHP website so having this project allows me to implement and test changes before modifying the PHP version. The second is to provide a way to generate formatted code snippets so I can display them within pages instead of having links to the Code Viewer and displaying an entire file.
The console application can also be automated so I can run a series of tests on it to ensure that I haven't broken some bit of formatting.
Interface
This is a command-line tool that takes several options and then performs the actions specified.
CodeFormater
Usage
CodeFormater.exe [?] || (Filepath [/nolines] [/noheader] [/header HeaderText] [/output OutputPath] [/start StartLine] [/end EndLine] [/type typeOverride])
Arguments
? – Display usage information, must be first argument
Filepath - The path to the file to format
/nolines - Output won't include line numbers
/noheader - Output won't include header
/header HeaderText – Displays HeaderText in the output header instead of the name of the file
/output OutputPath – Saves the output to OutputPath instead of the Filepath with the extension changed to html
/start StartLine – Start outputting at StartLine instead of the beginning of the file
/end EndLine – End outputting at EndLine (inclusive) instead of the end of the file
/type typeOverride – Used to specify the type to use instead of basing it off of the file extension
Code
CodeFormater.cpp
CodeFormater.cpp
This file contains the command line processing, file processing and output formatting code.
The main(int argc, char* argv[]) function is the start of the program and handles processing the command like arguments. It sets defaults for each of the parameters and then loops through the passed in arguments and updates the parameters based on the arguments found. If an error occurs with one of the arguments, the error will be printed along with usage instructions. Usage instructions are also printed if no arguments are given or if a ? is included. If the arguments are valid it then creates an instance of the CodeParser class and calls the ParseLines() method on it to generate a set of formatted line objects. If a non-zero number of lines are returned they are passed to the FormatLines() function to be printed to the output file.
The Ussage() function displays the list of allowed arguments and their description.
The FormatLines(std::vector<Line> lines, std::string outputPath, std::string header, bool noLines, bool noHeader) function is responsible for writing the formatted lines to the output file. The lines parameter contains the lines to be output and the rest of the parameters match the command line arguments. It starts by attempting to create the output file. If successful it then prints the header, if set to. It then prints the formatted lines in individual divs and then the line numbers if set to, before closing off the open divs. If no errors occur it returns true, otherwise it returns false.